<h2 class="popover-title">Thư đã gửi</h2>
<div id="notification">
  <?php if (isset($success)) { ?>
    <?php if ($success) { ?>
      <div class="alert alert-success"><?php echo $msg; ?></div>
    <?php } else { ?>
      <div class="alert alert-error"><?php echo $msg; ?></div>
    <?php } ?>
  <?php } ?>
</div>

<div id="action" style="margin-top: 10px;">
<!--   <form class="form-inline" style="float: left">
    <input type="text" class="input-small" placeholder="Tên người gửi">
    <input type="password" class="input-large" placeholder="Nội dung">
    <input type="text" class="input-small" placeholder="Ngày gửi">
    <button type="submit" class="btn">Lọc</button>
  </form> -->
  <a class="btn btn-success" href="<?php echo URL; ?>?page=viet_mail"><i class="icon-ok icon-white"></i> Soạn thư</a>
  <a class="btn btn-danger" id="btn_delete_mail"><i class="icon-trash icon-white"></i> Xóa</a>
</div>
<form id="list_form" action="<?php echo URL; ?>" method="get">
  <input type="hidden" name="page" value="mail_da_gui" />
  <input type="hidden" name="act" value="delete" />
  <table class="table table-striped table-bordered table-condensed" style="margin-top:10px">
  <thead>
  <tr>
    <th>
      <input type="checkbox" id="toogle_all" />
    </th>
    <th>
      #
    </th>
    <th class="yellow" width="330">
      Người nhận
    </th>
    <th class="blue">
      Tiêu đề
    </th>
    <th class="green" width="70">
      Ngày gửi
    </th>
  </tr>
  </thead>
  <tfoot>
    <tr>
      <td colspan="5" style="padding: 10px 0 10px 10px;">
        <span class="label">Nhãn</span>: Bạn bè &nbsp;&nbsp;&nbsp;
        <span class="label label-info">Nhãn</span>: Nhóm bạn bè
        <span class="label label-success">Nhãn</span>: Mail
      </td>
    </tr>
  </tfoot>
  <tbody>
    <?php $count = 0; foreach ($sent_mails as $sent_mail) { ?>
  <tr>
    <td><input type="checkbox" class="chk_sent_mail" name="sent_mails[]" value="<?php echo $sent_mail['sent_mail_id']; ?>" /></td>
    <td>
      <?php echo ++$count; ?>
    </td>
    <td style="line-height: 20px; vertical-align: middle;">
      <?php $receivers = $sent_mail['receiver']; ?>

      <?php foreach ($receivers['to'] as $to) { ?>
        <?php if ($to['friend_group_id']) { ?>
        <a class="label label-info"><?php echo $to['name']; ?></a>
        <?php } elseif ($to['friend_id']) { ?>
        <a class="label"><?php echo $to['fullname']; ?></a>
        <?php } else { ?>
        <span class="label label-success"><?php echo $to['mail']; ?></span> 
        <?php } ?>
      <?php } ?>

      <?php foreach ($receivers['cc'] as $cc) { ?>
        <?php if ($cc['friend_group_id']) { ?>
        <a class="label label-info">cc: <?php echo $cc['name']; ?></a>
        <?php } elseif ($cc['friend_id']) { ?>
        <a class="label">cc: <?php echo $cc['fullname']; ?></a>
        <?php } else { ?>
        <span class="label label-success">cc: <?php echo $cc['mail']; ?></span>
        <?php } ?>
      <?php } ?>

      <?php foreach ($receivers['bcc'] as $bcc) { ?>
        <?php if ($bcc['friend_group_id']) { ?>
        <a class="label label-info">bcc: <?php echo $bcc['name']; ?></a>
        <?php } elseif ($bcc['friend_id']) { ?>
        <a class="label">bcc: <?php echo $bcc['fullname']; ?></a>
        <?php } else { ?>
        <span class="label label-success">bcc: <?php echo $bcc['mail']; ?></span>
        <?php } ?>
      <?php } ?>
    </td>
    <td style="vertical-align: middle;">
      <a href="<?php echo URL; ?>?page=chi_tiet_mail&sent_mail_id=<?php echo $sent_mail['sent_mail_id']; ?>"><?php echo $sent_mail['subject']; ?></a>
    </td>
    <td style="text-align: center;">
      <?php echo $sent_mail['sent_date']; ?>
    </td>
  </tr>
  <?php } ?>
  </tbody>
  </table>
</form>
<script type="text/javascript">
  $('#btn_delete_mail').on('click', function (evt) {
    if ( !$('.chk_sent_mail').is(':checked') ) {
      alert('Chưa chọn mail để xóa');
      return;
    }

    if (confirm('Bạn có đồng ý xóa không?!')) {
      $('#list_form').submit();
    }
  });

  // checkall
  $('#toogle_all').on('click', function () {
    $('.chk_sent_mail').prop( 'checked', $(this).prop('checked') );
  });
</script>